package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.sql.Date;
import java.time.Instant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.GoodsAfterServiceDAO;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsAfterService;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsAfterService.Column;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsAfterServiceExample;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;


@Service
public class GoodsAfterServiceManager {

	@Autowired
	private GoodsAfterServiceDAO dao;
	
	private void verify(GoodsAfterService record) {
		BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()), ResultCode.VERIFY.build(10), "名称不能为空");
		BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getDetail()), ResultCode.VERIFY.build(11), "详情不能为空");
	}

	public GoodsAfterService add(GoodsAfterService record, String createUser) {
		verify(record);
		record.setDeleted(GoodsAfterService.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public GoodsAfterService edit(GoodsAfterService record, String updateUser) {
		verify(record);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted, Column.createUser, Column.createTime));
		return record;
	}

	public void delete(GoodsAfterService record, String updateUser) {
		record.setDeleted(GoodsAfterService.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
	}

	public GoodsAfterService getById(Integer afterServiceId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(afterServiceId, GoodsAfterService.NOT_DELETED);
	}

	public Pagination<GoodsAfterService> query(String name, Sorting[] sorts, Page page) {
		GoodsAfterServiceExample example = new GoodsAfterServiceExample();
		GoodsAfterServiceExample.Criteria criteria = example.createCriteria();

		if (Check.notNullOrTrimEmpty(name)) {
			criteria.andNameLike('%' + name
				+ '%');
		}

		criteria.andDeletedEqualTo(GoodsAfterService.NOT_DELETED);
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());

		return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
	}
}